﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.DataManagement.Cacheflow;
using Microsoft.Office.Tools.Excel;

namespace KOPLibrary.Forms.CashFlow
{
    public partial class FlowUc : CacheflowUserControlBase
    {
        private BindingSource paramSource;
        private string paramDataMember;
        private bool filtered = false;
        public Microsoft.Office.Interop.Excel.Application existingApp { get; set; }
        public FlowUc()
        {
            InitializeComponent();
        }



        private bool Filtered
        {
            get
            {
                return filtered;
            }

            set
            {
                filtered = value;
                if (value)
                {
                    ctlRemoveFilter.Visible = true;
                }
                else
                {
                    ctlRemoveFilter.Visible = false;
                    hareketBindingSource.RemoveFilter();
                }
            }
        }

        private void hareketBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            Commit();

        }

        public override void Commit()
        {
            base.Commit();
            this.Validate();
            this.hareketBindingSource.EndEdit();
            //this.tableAdapterManager.HareketTableAdapter.Update(this.currentDataSource.Hareket);
            DataLayer.SaveCachflowData(currentDataSource);
            //currentDataSource.Hareket.AcceptChanges();
        }


        private void FlowUc_Load(object sender, EventArgs e)
        {

        }

        public override void Bind(Cacheflow.CacheflowDs ds)
        {
            base.Bind(ds);
            hareketBindingSource.DataSource = ds;
        }

        public void Bind(Cacheflow.CacheflowDs ds, BindingSource source, string dataMember)
        {
            paramSource = source;
            paramDataMember = dataMember;
            Bind(ds);
            DataRowView current = hareketBindingSource.Current as DataRowView;
            ctlFilterUc.Bind(source, dataMember);
            if (current == null)
            {
                ctlSourceAcc.Init(source, "Ana HesapHesap");
                ctlDestAcc.Init(source, "Ana HesapHesap");
                ctlFlowType.SelectedIndex = 0;
            }
            else
            {
                ctlSourceAcc.Init(source, "Ana HesapHesap",
                    ((Cacheflow.CacheflowDs.HareketRow)current.Row).Kaynak_Ana_Hesap_ID,
                    ((Cacheflow.CacheflowDs.HareketRow)current.Row).Kaynak_Hesap_ID);
                ctlDestAcc.Init(source, "Ana HesapHesap",
    ((Cacheflow.CacheflowDs.HareketRow)current.Row).Hedef_Ana_Hesap_ID,
    ((Cacheflow.CacheflowDs.HareketRow)current.Row).Hedef_Hesap_ID);


            }

        }



        private void hareketBindingNavigatorSaveItem_Click_2(object sender, EventArgs e)
        {
            Commit();
            grid.Refresh();

        }

        private void ctlSourceAcc_Load(object sender, EventArgs e)
        {

        }

        private void hareketDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {


        }

        private void hareketBindingNavigator_RefreshItems(object sender, EventArgs e)
        {

        }

        private void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (hareketBindingSource.Current == null)
                return;
            if (e.ColumnIndex == 1)
            {
                var source = (DataView)(hareketBindingSource.List);
                if (e.RowIndex < source.Count)
                    e.Value = ((KOPLibrary.Cacheflow.CacheflowDs.HareketRow)source[e.RowIndex].Row).HesapBilgisi;
            }
        }

        private void hareketBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            DataRowView rView = (hareketBindingSource.List as DataView).AddNew();
            rView.Row["ID"] = Guid.NewGuid();
            if (hareketBindingSource.Current == null)
            {
                rView.Row["Tarih"] = DateTime.Now;
            }
            else if (((DataRowView)hareketBindingSource.Current).Row["Tarih"] is DBNull)
                rView.Row["Tarih"] = DateTime.Now;
            else
                rView.Row["Tarih"] = ((DataRowView)hareketBindingSource.Current).Row["Tarih"];
            rView.Row["Tölerans Yüzdesi"] = 0;
            rView.Row["Olasılık Yüzdesi"] = 100;
            numericUpDown1.Value = 100;
            numericUpDown2.Value = 0;

            e.NewObject = rView;
            hareketBindingSource.MoveLast();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            tutarTextBox.Focus();
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            Commit();
        }

        private void tutarLabel_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (hareketBindingSource.Current == null)
                MessageBox.Show("Bu işlemin geçerli olabilmesi için aktif nakit akış bilgisi gerekmektedir.");
            else
            {
                MultipleFlowForm form = new MultipleFlowForm();
                int result = form.Execute(currentDataSource, paramSource, paramDataMember, hareketBindingSource.Current == null ? null : ((DataRowView)hareketBindingSource.Current).Row as KOPLibrary.Cacheflow.CacheflowDs.HareketRow);
                if (result > 0)
                {
                    Commit();
                    MessageBox.Show(string.Format("Toplam {0} adet düzenli nakit akış hareketi oluşturuldu.", result));
                }
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }





        private void açıklamaTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void ctlRemoveFilter_Click(object sender, EventArgs e)
        {
            Filtered = false;

        }

        private void ctlFilter_Click(object sender, EventArgs e)
        {
            hareketBindingSource.Filter = ctlFilterUc.GetLocalFilterExpression();
            if (string.IsNullOrEmpty(hareketBindingSource.Filter))
                Filtered = false;
            else Filtered = true;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            analizTablosuToolStripMenuItem_Click(sender, e);
        }

        private void analizTablosuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            ExcelExporter exporter = new ExcelExporter(app, ExportType.CreateNew);
            exporter.CreateAnalyse((((DataView)hareketBindingSource.List)));            
            ParentForm.Close();
            app.Visible = true;
        }

        private void analizGrafiğiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExcelExporter exporter = new ExcelExporter(existingApp, ExportType.UpdateExsiting);
            exporter.CreateAnalyse((((DataView)hareketBindingSource.List)));
            ParentForm.Close();
        }

        private void sadeceVeriAktarımıToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            ExcelExporter exporter = new ExcelExporter(app, ExportType.DataOnly);
            exporter.CreateAnalyse((((DataView)hareketBindingSource.List)));
            app.Visible = true;
 
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void toolStripButton2_DropDownOpening(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Worksheet sheet;
            try
            {
                sheet = (Microsoft.Office.Interop.Excel.Worksheet)existingApp.ActiveWorkbook.Sheets[ExcelExporter.DataSheetName];
                analizGrafiğiToolStripMenuItem.Visible = true;
            }
            catch
            {
                analizGrafiğiToolStripMenuItem.Visible = false;
            }
        }

    }
}
